package cn.ts.android.attendance.lock;


import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 */
public class ListWithLock<T> extends ObjWithLock<List<T>> {
    private static final long serialVersionUID = 8549668315606224029L;

    /**
     */
    public ListWithLock(List<T> list) {
        super(list);
    }

    /**
     * @param lock
     */
    public ListWithLock(List<T> list, ReentrantReadWriteLock lock) {
        super(list, lock);
    }


    /**
     *
     * @return
     */
    public boolean add(T t) {
        ReentrantReadWriteLock.WriteLock writeLock = this.getLock().writeLock();
        writeLock.lock();
        try {
            List<T> list = this.getObj();
            return list.add(t);
        } catch (Throwable e) {
            System.out.println(e.getMessage());
        } finally {
            writeLock.unlock();
        }
        return false;
    }

    /**
     *
     *
     */
    public void clear() {
        ReentrantReadWriteLock.WriteLock writeLock = this.getLock().writeLock();
        writeLock.lock();
        try {
            List<T> list = this.getObj();
            list.clear();
        } catch (Throwable e) {
            System.out.println(e.getMessage());
        } finally {
            writeLock.unlock();
        }
    }

    /**
     *
     */
    public boolean remove(T t) {
        ReentrantReadWriteLock.WriteLock writeLock = this.getLock().writeLock();
        writeLock.lock();
        try {
            List<T> list = this.getObj();
            return list.remove(t);
        } catch (Throwable e) {
            System.out.println(e.getMessage());
        } finally {
            writeLock.unlock();
        }
        return false;
    }

    /**
     *
     */
    public int size() {
        ReentrantReadWriteLock.ReadLock readLock = this.getLock().readLock();
        readLock.lock();
        try {
            List<T> list = this.getObj();
            return list.size();
        } finally {
            readLock.unlock();
        }
    }
}